SuperStrict

Framework maxgui.drivers
Import brl.eventqueue
Import "ScintillaCtrl.bmx"


'**************************Sample***************************
Local window:TGadget
Local textarea:TScintillaGadget ' TGadget works as well, but then we need to cast when using scintilla specific stuff

window=CreateWindow("My Window",130,20,800,600,,15|WINDOW_ACCEPTFILES)
Local tabber:TGadget
Local document:TGadget

Local filemenu:TGadget
Local editmenu:TGadget
Local editmenuPOP:TGadget
Local helpmenu:TGadget

Const MENU_NEW:Int=101
Const MENU_OPEN:Int=102
Const MENU_SAVE:Int=103
Const MENU_CLOSE:Int=104
Const MENU_EXIT:Int=105

Const MENU_CUT:Int=106
Const MENU_COPY:Int=107
Const MENU_PASTE:Int = 108
Const MENU_UNDO:Int = 109
Const MENU_REDO:Int = 110
Const MENU_FONT:Int = 111

Const MENU_ABOUT:Int=112


filemenu=CreateMenu("&File",0,WindowMenu(window))
CreateMenu"&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND
CreateMenu"&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND
CreateMenu"&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

editmenu = CreateMenu("&Edit" , 0 , WindowMenu(window) )
CreateMenu "&Undo",MENU_UNDO,editmenu,KEY_U,MODIFIER_COMMAND
CreateMenu "&Redo",MENU_REDO,editmenu,KEY_R,MODIFIER_COMMAND
CreateMenu"",0,editmenu
CreateMenu "Cu&t",MENU_CUT,editmenu,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenu,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste" , MENU_PASTE , editmenu , KEY_V , MODIFIER_COMMAND
CreateMenu "Font",MENU_FONT,editmenu

editmenuPop = CreateMenu("&Edit" , 0 , Null)
CreateMenu "&Undo",MENU_UNDO,editmenuPop ,KEY_U,MODIFIER_COMMAND
CreateMenu "&Redo",MENU_REDO,editmenuPop ,KEY_R,MODIFIER_COMMAND
CreateMenu"",0,editmenuPop 
CreateMenu "Cu&t",MENU_CUT,editmenuPop ,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenuPop ,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste",MENU_PASTE,editmenuPop ,KEY_V,MODIFIER_COMMAND

helpmenu=CreateMenu("&Help",0,WindowMenu(window))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu window

' CreateDocument creates a hidden panel that fills entire tabber client area 

Function CreateDocument:TGadget(tabber:TGadget)
	Local	panel:TGadget
	panel=CreatePanel(0,0,ClientWidth(tabber),ClientHeight(tabber),tabber)
	SetGadgetLayout panel,1,1,1,1
	HideGadget panel
	Return panel
End Function



tabber=CreateTabber(0,0,ClientWidth(window),ClientHeight(window),window)
SetGadgetLayout tabber,1,1,1,1 

document = CreateDocument(tabber)

ShowGadget document

textarea = CreateScintillaArea(,,,,document)
SetGadgetLayout textarea , 1 , 1 , 1 , 1
Local text:String = LoadText("ScintillaCtrl.bmx")
Print "Text length: " + textarea.charcount();
SetTextAreaText textarea,text
textarea.SetLineNumbering(0 , True)
textarea.SetMarginType(1, SC_MARGIN_SYMBOL, 20)
textarea.SetMarginSensitive(1 , True)
textarea.SendEditor(SCI_USEPOPUP , 0) 'Deactivate Scintillas own popup

SetGadgetLayout textarea,1,1,1,1
ActivateGadget textarea

Global keys:String = ""
Global keymap:TMap = New TMap

Local cmds:TStream = ReadStream("commands.txt")

While Not Eof(cmds)
	Local L:String = ReadLine(cmds).Trim()
	Local Key:String = l[l.FindLast("#") + 1..]
	Print Key
	If Key.toLower().Contains("extends") Or Key.Contains(":")
		Key = Key[..Key.Find(" ")]
	End If
	keys:+ Key.toLower() + " "
	keymap.Insert(Key.toLower(),Key)
Wend


'Highlighting stuff
textarea.SetLexer(SCLEX_BLITZMAX)
textarea.SetLexerKeywords(0 , keys)

For Local I:Int = 0 To 19
	textarea.SetLexerStyle(SCI_STYLESETFORE , I , EncodeColor(255,255,255))
	textarea.SetLexerStyle(SCI_STYLESETBACK , I , EncodeColor(1 , 81 , 107) )
Next

textarea.SendEditor(SCI_SETCARETFORE,EncodeColor(255,255,255))

textarea.SetLexerStyle(SCI_STYLESETFORE , SCE_B_KEYWORD , EncodeColor(255,255,0))
textarea.SetLexerStyle(SCI_STYLESETBACK , SCE_B_KEYWORD, EncodeColor(1,81,107)) 
textarea.SetLexerStyle(SCI_STYLESETFORE , SCE_B_MULTILINECOMMENT , EncodeColor(177,231,235))
textarea.SetLexerStyle(SCI_STYLESETFORE , SCE_B_COMMENT , EncodeColor(177 , 231 , 235) )
textarea.SetLexerStyle(SCI_STYLESETFORE , SCE_B_NUMBER , EncodeColor(21 , 255 , 185) )
textarea.SetLexerStyle(SCI_STYLESETFORE , SCE_B_STRING , EncodeColor(0 , 255 , 107) )
textarea.SetLexerStyle(SCI_STYLESETBACK , STYLE_DEFAULT , EncodeColor(1 , 81 , 107) )

textarea.SendEditor(SCI_SETMARGINMASKN , 1 , SC_MASK_FOLDERS) ;

textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_ARROW);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_ARROWDOWN);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND,  SC_MARK_EMPTY);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
textarea.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL,SC_MARK_EMPTY );
textarea.SendEditor(SCI_SETFOLDFLAGS , 16 , 0)

textarea.SendEditor(SCI_SETINDENTATIONGUIDES,SC_IV_LOOKBOTH)

textarea.SetProperty("fold","1")
textarea.SetProperty("fold.compact" , "0")

'textarea.DoPrint()

Print textarea.GetLexer()

SelectTextAreaText textarea,0,0,TEXTAREA_LINES

Print "C : " + TextAreaCursor(textarea,TEXTAREA_CHARS) 
Print "L : " + TextAreaSelLen(textarea,TEXTAREA_CHARS) 


'Print "Text : " +TextAreaText(textarea,TextAreaCursor(textarea,TEXTAREA_CHARS) ,TextAreaSelLen(textarea,TEXTAREA_CHARS) , TextArea_CHARS)

'SetTextAreaText textarea,"Hello World",20,50

SetGadgetFilter(textarea , filter, textarea)

Function filter:Int(event:TEvent,context:Object)
	Select event.id
		Case EVENT_KEYCHAR
			'Lets do a more precise UNDO REDO
			'A bit buggy , it adds 2 undos at once, don't know why
			'but it should give an idea
			Local textarea:TScintillaGadget = TScintillaGadget(context)
			If textarea <> Null Then
				'Local line:Int = TextAreaCursor(textarea,TEXTAREA_LINES)
				'textarea.SendEditor(SCI_ADDUNDOACTION , line)	
				
				'Checking AutoList
				Local curPosition:Int = TextAreaCursor(textarea)
				Local s:Int = textarea.SendEditor(SCI_WORDSTARTPOSITION,curPosition,1)
				Local e:Int = textarea.SendEditor(SCI_WORDENDPOSITION,curPosition,1)
				Local l:Int = 0
				Local token:String 
				Try
				If event.data > 32 And event.data < 126 Then
					token = TextAreaText(textarea , s , (e - s) , TEXTAREA_CHARS) + Chr(event.data)
					l = token.length -1
				Else
					token = TextAreaText(textarea , s , (e - s) , TEXTAREA_CHARS)
					l=token.length
				End If
				Catch ex:String
					Print "Exception: " + ex
				End Try
			'	Print "Token : " + token + " : " + token.length +";"+ textarea.SendEditor(SCI_AUTOCACTIVE)
				
				Local count:Int = 0
				If token.length > 0 And textarea.SendEditor(SCI_AUTOCACTIVE) = 0 Then
					Local keylist:String = ""
					For Local T:String = EachIn keymap.Values()
						'Print token.Trim().toLower() + " : " + token.length
						If t.ToLower().StartsWith(token.toLower() ) Then
							keylist:+ t + " "
							count:+1
						End If
					Next
				'	Print "Items: " + count
					textarea.SendEditor(SCI_AUTOCSETIGNORECASE , 1)
					textarea.SendEditor(SCI_AUTOCSETMAXHEIGHT,10)
					textarea.SendEditorString(SCI_AUTOCSHOW , l , keylist)
				
				End If
			End If
	End Select
	Return 1
End Function

Local Font:TGuiFont = Null

While WaitEvent()
	'Print CurrentEvent.ToString()+" "+EventSourceHandle()
	Select EventID()
		Case EVENT_GADGETMENU
			PopupWindowMenu(window,editmenuPOP)
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_APPTERMINATE
			End
		Case EVENT_GADGETACTION
			'Removed Scintilla Eventhandling from main code now everything needs to be handled in the
			'maxgui eventsystem
			If EventSource() = textarea Then
				Local n:TScintillaEventData = TScintillaEventData(EventExtra() )
				Select n.Code
					Case SCN_MARGINCLICK
						Local Line:Int = textarea.SendEditor(SCI_LINEFROMPOSITION , n.position , 0) ;
						Local levelClick:Int = textarea.SendEditor(SCI_GETFOLDLEVEL , line) ;
						If levelClick & SC_FOLDLEVELHEADERFLAG <> 0 Then
							textarea.SendEditor(SCI_TOGGLEFOLD , Line , 0)
						End If
					Case SCN_MODIFIED
						'Print "Char : " + n.ch
				End Select
				
			End If
			
		Case EVENT_MENUACTION
			Select EventData()
				Case MENU_UNDO
					textarea.SendEditor(SCI_UNDO)
				Case MENU_REDO
					textarea.SendEditor(SCI_REDO)
				Case MENU_CUT
					textarea.SendEditor(SCI_CUT)
				Case MENU_COPY
					textarea.SendEditor(SCI_COPY)
				Case MENU_PASTE
					textarea.SendEditor(SCI_PASTE)
				Case MENU_FONT
					DebugStop()
					font = RequestFont(font)
					If font
					SetGadgetFont textarea, font
				   	 EndIf
				Case MENU_EXIT
					End
				Case MENU_ABOUT
					Notify "Scintilla~nBlitzmaxGadget"
			End Select
	End Select
Wend
